#include "Common/Utility/ServerTime.h"
#include "base/core/com_define.h"
#include "base/core/os/time.h"
#include "base/core/os/debug.h"

uint64_t ServerTime::ms_startTick = 0;
uint64_t ServerTime::ms_curTick   = 0;
uint64_t ServerTime::ms_lastTick  = 0;
int      ServerTime::ms_deltaTick = 0;
uint64_t ServerTime::ms_unixSec   = 0;
uint64_t ServerTime::ms_frames    = 0;
uint64_t ServerTime::ms_secs      = 0;
bool     ServerTime::ms_perSecFirstFrame = 0;
uint64_t ServerTime::ms_lastUnixSec = 0;
int      ServerTime::ms_fps       = 0;

ServerTime::ServerTime() 
{}

ServerTime::~ServerTime() 
{}

bool ServerTime::Init(int fps) 
{
    ms_fps       = fps;
    ms_curTick   = Time::Now().Tick();
    ms_startTick = ms_curTick;
    ms_lastTick  = ms_curTick;
    ms_deltaTick = 0;
    ms_unixSec   = Time::Now().UnixSec();
    ms_frames    = 0;
    ms_secs      = 0;
    ms_perSecFirstFrame = false;
    ms_lastUnixSec = ms_unixSec;
    return true;
}

bool ServerTime::UnInit() 
{
    return true;
}

bool ServerTime::Update(uint64_t tick) 
{
    ms_lastTick  = ms_curTick;
    ms_curTick   = tick;
    ms_deltaTick = (int)(ms_curTick - ms_lastTick);
    ms_unixSec   = Time::Now().UnixSec();

    ++ms_frames;

    if (ms_lastUnixSec - ms_unixSec > 0) 
	{
        ms_secs += ms_unixSec - ms_lastUnixSec;
        ms_lastUnixSec = ms_unixSec;
        ms_perSecFirstFrame = true;
    }
    else 
	{
        ms_perSecFirstFrame = false;
    }

    return true;
}
